/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Data.KhachHang;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author bienphongvnn
 */
public class checkRegister {

    public static ResultSet rs = null;
    public static PreparedStatement pst = null;

    public static void ThemTaiKhoan(KhachHang n) {
        try {
            Connection conn = ConnectionProvider.Connect();
            String sql = "INSERT INTO khachhang(TenDangNhap, HoTen, CMND, NgaySinh, GioiTinh, DiaChi, DienThoai, Email, MatKhau, XacNhanMatKhau) VALUES(?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, n.TenDangNhap);
            pst.setString(2, n.HoTen);
            pst.setString(3, n.CMND);
            pst.setDate(4, n.NgaySinh);
            pst.setInt(5, n.GioiTinh);
            pst.setString(6, n.DiaChi);
            pst.setString(7, n.DienThoai);
            pst.setString(8, n.Email);
            pst.setString(9, n.MatKhau);
            pst.setString(10, n.XacNhanMatKhau);
            pst.executeUpdate();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
        }
    }

    public boolean checkUser(String tendangnhap,String email) {
        rs = Login(tendangnhap, email);
        try {
            if (rs.next()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            ex.getMessage();
            return false;
        }
    }

    public static ResultSet Login(String tendangnhap,String email) {
        try {
            Connection conn = ConnectionProvider.Connect();
            String sql = "Select * from khachhang where TenDangNhap = ? and Email = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1, tendangnhap);
            pst.setString(2, email);
            return rs = pst.executeQuery();
        } catch (Exception ex) {
            return null;
        }
    }
}
